# encoding = utf-8

from application.logging import logger
from application.model.v3_model.CV_Parameter_online.CV_gamma import CV_gamma
from application.model.v3_model.CV_Parameter_online.CV_lambda import CV_lambda
from application.model.v3_model.CV_Parameter_online.CV_learn import CV_learn
from application.model.v3_model.CV_Parameter_online.CV_n_estimators import CV_n_estimators
from application.model.v3_model.CV_Parameter_online.CV_sub_sample import CV_sub_sample
from application.model.v3_model.CV_Parameter_online.CV_weight import CV_weight
from application.utils.CodeTimingUtil import CodeTimingUtil


@CodeTimingUtil(name="[调参]CV_parameter")
def CV_parameter(train_x, train_y):
    """
    调参
    :param train_x: 训练集x
    :param train_y: 训练集y
    :return:
    """
    #
    logger.info(f"调参进度[1/6]:开始")
    n_estimators = CV_n_estimators(train_x=train_x, train_y=train_y)
    logger.info(f"调参进度[1/6]:结束")

    #
    logger.info(f"调参进度[2/6]:开始")
    max_depth, min_child_weight = CV_weight(train_x=train_x, train_y=train_y, n_estimators=n_estimators)
    logger.info(f"调参进度[2/6]:开始")

    #
    logger.info(f"调参进度[3/6]:开始")
    gamma = CV_gamma(train_x=train_x, train_y=train_y, n_estimators=n_estimators, max_depth=max_depth,
                     min_child_weight=min_child_weight)
    logger.info(f"调参进度[3/6]:开始")

    #
    logger.info(f"调参进度[4/6]:开始")
    subsample, colsample_bytree = CV_sub_sample(train_x=train_x, train_y=train_y, n_estimators=n_estimators,
                                                max_depth=max_depth, min_child_weight=min_child_weight, gamma=gamma)
    logger.info(f"调参进度[4/6]:开始")

    #
    logger.info(f"调参进度[5/6]:开始")
    reg_alpha, reg_lambda = CV_lambda(train_x=train_x, train_y=train_y, n_estimators=n_estimators, max_depth=max_depth,
                                      min_child_weight=min_child_weight, gamma=gamma, subsample=subsample,
                                      colsample_bytree=colsample_bytree)
    logger.info(f"调参进度[5/6]:开始")

    #
    logger.info(f"调参进度[6/6]:开始")
    learning_rate = CV_learn(train_x=train_x, train_y=train_y, n_estimators=n_estimators, max_depth=max_depth,
                             min_child_weight=min_child_weight, gamma=gamma, subsample=subsample,
                             colsample_bytree=colsample_bytree, reg_alpha=reg_alpha,
                             reg_lambda=reg_lambda)
    logger.info(f"调参进度[6/6]:开始")

    #
    best_param = {
        "n_estimators": n_estimators,
        "max_depth": max_depth,
        "min_child_weight": min_child_weight,
        "gamma": gamma,
        "subsample": subsample,
        "colsample_bytree": colsample_bytree,
        "reg_alpha": reg_alpha,
        "reg_lambda": reg_lambda,
        "learning_rate": learning_rate,
    }
    # 调参结果打印
    logger.info(f"调参结果:{'*' * 50}")
    for key in best_param.keys():
        logger.info(f"调参结果[{key}] : {best_param.get(key)}")
        pass
    logger.info(f"调参结果:{'*' * 50}")
    # 返回结果
    return best_param
    pass


if __name__ == '__main__':
    pass
